package cn.gwm.utils;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName DateTimeUtil
 * @Author xzh
 * @Date 2022/10/31 10:36
 **/
public class DateTimeUtil {

    /**
     * 标准日期格式
     **/
    public final static String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";


    /**
     * @return long 毫秒数
     * @Author xzh
     * @Description 默认日期格式返回时间毫秒数
     * @Date 2022/10/31 10:41
     * @Param [time] 字符串日期
     **/
    public static long defaultFormatPare(String time) {
        return defaultFormatPareDate(time).getTime();
    }

    /**
     * @return java.util.Date
     * @Author xzh
     * @Description 默认日期格式返回时间
     * @Date 2022/10/31 10:46
     * @Param [time]
     **/
    public static Date defaultFormatPareDate(String time) {
        return pareDate(time, DATE_TIME_FORMAT);
    }

    /**
     * @return long
     * @Author xzh
     * @Description 指定日期格式返回时间毫秒数
     * @Date 2022/10/31 10:41
     * @Param [time, dateFormat]
     **/
    public static long pareLongDate(String time, String dateFormat) {
        return pareDate(time, dateFormat).getTime();
    }

    /**
     * @return java.util.Date
     * @Author xzh
     * @Description 指定日期格式返回时间
     * @Date 2022/10/31 10:47
     * @Param [time, dateFormat]
     **/
    public static Date pareDate(String time, String dateFormat) {
        Date date = new Date();
        try {
            date = new SimpleDateFormat(dateFormat).parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }


    public static Date getDateOffsetYear(Date nowDate) {
        int currentYear = DateUtil.year(new Date());
        return DateUtil.offset(nowDate, DateField.YEAR, currentYear - DateUtil.year(nowDate));

    }

    /**
     * 处理开始结束日期，年月问题
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static Date[] getStartDateAndEndDate(Date startDate, Date endDate) {
        // 判断开始月份是否大于结束月份
        if (DateUtil.month(startDate) > DateUtil.month(endDate)) {
            System.out.printf("设置时间");
            // 设置开始日期的年份为当前年
            startDate = DateUtil.offset(startDate, DateField.YEAR, DateUtil.year(DateUtil.date()) - DateUtil.year(startDate));
            // 设置结束日期的年份为当前年的下一年
            endDate = DateUtil.offset(endDate, DateField.YEAR, 1 - DateUtil.year(endDate) + DateUtil.year(DateUtil.date()));
        } else {
            startDate = DateUtil.offset(startDate, DateField.YEAR, DateUtil.year(DateUtil.date()) - DateUtil.year(startDate));
            endDate = DateUtil.offset(endDate, DateField.YEAR, DateUtil.year(DateUtil.date()) - DateUtil.year(endDate));
        }
        endDate = DateUtil.offsetHour(endDate, 23);
        endDate = DateUtil.offsetMinute(endDate, 59);
        endDate = DateUtil.offsetSecond(endDate, 59);
        System.out.printf("startDate: " + startDate + " endDate: " + endDate);
        System.out.printf("\n");
        return new Date[]{startDate, endDate};

    }

    public static void main(String[] args) {
        getStartDateAndEndDate(new Date(1640880000000L), new Date(1651161600000L));
        getStartDateAndEndDate(new Date(1651248000000L), new Date(1667059200000L));
        getStartDateAndEndDate(new Date(1667145600000L), new Date(1672329600000L));
    }

    public static DateTime getDateTime(Date nowDate) {
        return DateUtil.parse(DateUtil.format(nowDate, "HH:mm:ss"));
    }

    /**
     * @param nowDate   要比较的时间
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @return true在时间段内，false不在时间段内
     * @throws Exception
     */
    public static boolean hourMinuteBetween(String nowDate, String startDate, String endDate) {
        System.out.println("nowDate = " + nowDate + " startDate  = " + startDate + " endDate  = " + endDate);
        DateTime now = getDateTime(DateUtil.parse(nowDate));
        DateTime start = getDateTime(DateUtil.parse(startDate));
        DateTime end = getDateTime(DateUtil.parse(endDate));
        System.out.println("nowDate = " + now + " startDate  = " + start + " endDate  = " + end);
        // 跨天情况
        if (start.hour(true) > end.hour(true) ||
                (start.hour(true) == end.hour(true) && start.minute() >= end.minute())
        ) {
            end.offset(DateField.DAY_OF_YEAR, 1);
            // 跨天且当前时间大于结束时间
            if (end.hour(true) > now.hour(true) ||
                    (end.hour(true) == now.hour(true) && end.minute() >= now.minute())
            ) {
                now.offset(DateField.DAY_OF_YEAR, 1);
            }
        }
        long nowTime = now.getTime();
        long startTime = start.getTime();
        long endTime = end.getTime();
        return nowTime >= startTime && nowTime <= endTime;
    }
}
